//; mapshader.fx written by Alex Anderson
//; @15.8.12

// ################
// ### Varibles ###
float4x4 World;
float4x4 View;
float4x4 Projection; 

float4 AmbientColor;
float AmbientLevel;

float3 DiffuseDirection;
float4 DiffuseColor;
float4 DiffuseLevel;

float TimeStamp;
bool OssilateY;
float OssilateYMultipler;

Texture StructureTexture;
sampler TextureSampler = sampler_state { texture = <StructureTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};

// #######################
// ### Data Structures ###
struct VertexData {
	float4 Position : POSITION;	
	float3 UV : TEXCOORD0;
	float3 Normal : TEXCOORD1;
};

struct PixelData {
	float4 Color : COLOR0;
};

// ########################
// ### Shader Functions ###
VertexData BlueprintVertexShader(float4 inPos : POSITION0, float3 inUV : TEXCOORD0)
{
	VertexData output = (VertexData)0;

	float4x4 vp = mul(View, Projection);
	float4x4 wvp = mul(World, vp);
	float4 position = mul(inPos, wvp).xyzw;
	position.y += 3;
	output.Position = position;
	output.UV = inUV;

	return output;
}
VertexData StructVertexShader(float4 inPos : POSITION0, float3 inNormal: NORMAL0, float3 inUV : TEXCOORD0)
{
	VertexData output = (VertexData)0;

	float4x4 vp = mul(View, Projection);
	float4x4 wvp = mul(World, vp);
	float4 position = mul(inPos, wvp).xyzw;

	if(OssilateY)
		position.y += (OssilateYMultipler * sin(TimeStamp / 160));

	
	output.Position = position;
	output.UV = inUV;
	output.Normal = normalize(mul(inNormal, (float3x3)World));

	return output;
}


PixelData BlueprintPixel(VertexData inData)
{
	PixelData output = (PixelData)0;		
	output.Color = float4(0, 148, 255, 128) * (AmbientColor * AmbientLevel);
	return output;
} 

PixelData TextureShader(VertexData inData)
{
	PixelData output = (PixelData)0;	 
	float4 texturecolor = tex2D(TextureSampler, inData.UV).rgba;
	texturecolor.a = 1.f;

	float4 norm = float4(inData.Normal, 1.f);
	float4 diffuse = saturate(dot(-DiffuseDirection, norm));

	output.Color = texturecolor;
		//(texturecolor * (AmbientColor * AmbientLevel)); // +
		//(texturecolor * (DiffuseLevel * DiffuseColor * diffuse)); 

	return output;
} 

// ##################
// ### Techniques ###
technique BlueprintShade
{
	pass Pass0
	{
		VertexShader = compile vs_2_0 BlueprintVertexShader();
		PixelShader = compile ps_2_0 BlueprintPixel();
	}
}
technique TextureShade
{
	pass Pass0
	{
		VertexShader = compile vs_2_0 StructVertexShader();
		PixelShader = compile ps_2_0 TextureShader();
	}
}